package com.dz.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dz.risk.common.ResponseVO;
import com.dz.risk.common.SysConstant;
import com.dz.risk.dao.IndicatorDetailsDao;
import com.dz.risk.pojo.domain.IndicatorDetailsEntity;
import com.dz.risk.pojo.domain.TemporaryTableOfDataEntity;
import com.dz.risk.pojo.model.PageModel;
import com.dz.risk.service.IndicatorDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("indicatorDetailsService")
public class IndicatorDetailsServiceImpl extends ServiceImpl<IndicatorDetailsDao, IndicatorDetailsEntity> implements IndicatorDetailsService {

    @Autowired
    private IndicatorDetailsDao indicatorDetailsDao;

    /**
     * 查询所有数据
     * @param queryParams
     * @return
     */
    @Override
    public IPage<IndicatorDetailsEntity> query( PageModel pageModel,IndicatorDetailsEntity queryParams) {
        IPage<IndicatorDetailsEntity> page = new Page<>();
        page.setCurrent(pageModel.getPageNum());
        page.setSize(pageModel.getPageSize());
        QueryWrapper<IndicatorDetailsEntity> wrapper = new QueryWrapper<IndicatorDetailsEntity>();
        wrapper.lambda()
                .like(!StringUtils.isEmpty(queryParams.getIndexNumber()), IndicatorDetailsEntity::getIndexNumber, queryParams.getIndexNumber())
                .like(!StringUtils.isEmpty(queryParams.getIndexName()), IndicatorDetailsEntity::getIndexName, queryParams.getIndexName()).orderByDesc(IndicatorDetailsEntity::getIndexNumber);
        page = indicatorDetailsDao.selectPage(page, wrapper);
        page.setTotal(page.getTotal());
        return page;
    }

    /**
     * 通过indexNumber查询对象
     * @param indexNumber
     * @return
     */
    @Override
    public IndicatorDetailsEntity get(String indexNumber) {
        return indicatorDetailsDao.selectGetIndexNum(indexNumber);
    }

    /**
     * 添加
     * @param indicatorDetailsEntity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseVO add(IndicatorDetailsEntity indicatorDetailsEntity) {
        ResponseVO responseVO = queryOnly(indicatorDetailsEntity);
        if (responseVO != null) {
            return responseVO;
        }
        indicatorDetailsEntity.setCreatedTime(new Date());
        indicatorDetailsDao.insert(indicatorDetailsEntity);
        return ResponseVO.ok();
    }

    public ResponseVO queryOnly(IndicatorDetailsEntity indicatorDetailsEntity) {
        List<IndicatorDetailsEntity> detailsEntityList = indicatorDetailsDao.selectByIdAndName(indicatorDetailsEntity.getIndexNumber(), indicatorDetailsEntity.getIndexName());
        for (IndicatorDetailsEntity detailsEntity : detailsEntityList) {
             if (detailsEntity.getIndexNumber().equals(indicatorDetailsEntity.getIndexNumber())){
                return ResponseVO.fail("已有指标编号 : " + detailsEntity.getIndexNumber());
            }
        }
        return null;
    }

    @Override
    public List<IndicatorDetailsEntity> selectAll() {
        QueryWrapper<IndicatorDetailsEntity> wrapper = new QueryWrapper<IndicatorDetailsEntity>();

        return indicatorDetailsDao.selectList(wrapper);
    }

    @Override
    public List<String> selectIndexIds() {
        return indicatorDetailsDao.selectIndexIds();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFormulaMap(Map<Long, String> hashMap) {
        indicatorDetailsDao.updateFormulaMap(hashMap);
    }

    @Override
    public void deleteById(String indexNumber) {
        indicatorDetailsDao.deleteByIndexNumber(indexNumber);
    }

}